//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package common;

import farm.Worker;
import messages.Login;
import objects.LogicEventData;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

public class FarmsWork implements Runnable {
    private final cfgFile profiles = new cfgFile("currentfarmwork.cfg");
    private static final List<Login> accounts = new ArrayList();
    private static final List<Worker> workers = new ArrayList();
    private final Thread thr;
    private final String workerprofilesname = "currentfarmwork.cfg";
    private volatile boolean dostop = false;
    SecureRandom rnd = new SecureRandom();
    NameGenerator ng = new NameGenerator();
    boolean doTriplesOnExit = false;

    public FarmsWork(Integer farmcount) {
        if(farmcount.intValue() > accounts.size()) {
            int needfarms = farmcount.intValue() - accounts.size();
            System.out.println(Logger.formatPT("Collector", "  create " + needfarms + " new accounts."));

            for(int i = 0; i < needfarms; ++i) {
                Login anew = this.createNewFarm((Integer)null, Louncher.resourcesha);
                accounts.add(anew);
                anew.FarmName = this.ng.getNextName(7 + this.rnd.nextInt(8));
            }
        }

        System.out.println(Logger.formatPT("Collector", "  start FarmsWorker thread."));
        this.thr = new Thread(this, "FarmsWorker");
        this.thr.start();
    }

    public static void addAccount(Login l) {
        List var1 = accounts;
        synchronized(accounts) {
            accounts.add(l);
        }
    }

    public static Login getAccount(int index) {
        List var1 = accounts;
        synchronized(accounts) {
            return index < accounts.size()?(Login)accounts.get(index):null;
        }
    }

    public static int accountsSize() {
        List var0 = accounts;
        synchronized(accounts) {
            return accounts.size();
        }
    }

    public static void changeAccount(int index, Login loginData) {
        List var2 = accounts;
        synchronized(accounts) {
            if(index < accounts.size()) {
                Login oldacc = (Login)accounts.get(index);
                loginData.FarmName = oldacc.FarmName;
                accounts.set(index, loginData);
            }

        }
    }

    private void writeToConfig(int index, cfgFile cfg, Login data, String name, boolean writeToken) {
        Long AccountID = Long.valueOf(data.AccountID.longValue());
        Integer groupIndex = Integer.valueOf(AccountID.intValue() % LogicEventData.getEvent(0).getGroupSize().intValue());
        ByteStream bs = new ByteStream(100);
        data.partialEncode(bs);
        cfg.setProperty(String.format("profile.name.%d", new Object[]{Integer.valueOf(index)}), name);
        cfg.setProperty(String.format("profile.accountid.%d", new Object[]{Integer.valueOf(index)}), String.valueOf(data.AccountID.longValue()));
        if(writeToken) {
            cfg.setProperty(String.format("profile.accountToken.%d", new Object[]{Integer.valueOf(index)}), data.PassToken);
        }

        cfg.setProperty(String.format("profile.login.data.%d", new Object[]{Integer.valueOf(index)}), StringUtils.hexToString(bs.getCopyOfByteArray()));
        cfg.setProperty(String.format("profile.groupindex.%d", new Object[]{Integer.valueOf(index)}), groupIndex.toString());
    }

    public void saveProfiles() {
        for(int groups = 0; groups < accounts.size(); ++groups) {
            this.writeToConfig(groups, this.profiles, (Login)accounts.get(groups), ((Login)accounts.get(groups)).FarmName, false);
        }

        this.profiles.setProperty("profile.size", String.valueOf(accounts.size()));

        try {
            this.profiles.cfgstore("#Farm Workers");
            System.out.println("Done.");
        } catch (FileNotFoundException var16) {
            var16.printStackTrace();
        } catch (IOException var17) {
            var17.printStackTrace();
        }

        if(this.doTriplesOnExit) {
            System.out.println(Logger.formatPT("Collector", "  Triples selection process..."));
            LinkedHashMap var18 = new LinkedHashMap();
            TripleGroups triples = new TripleGroups();
            ArrayList accountGroups = new ArrayList();

            int triple;
            Long AccountID;
            for(triple = 0; triple < accounts.size(); ++triple) {
                if(((Login)accounts.get(triple)).PassToken != null && ((Login)accounts.get(triple)).PassToken.length() > 0) {
                    AccountID = Long.valueOf(((Login)accounts.get(triple)).AccountID.longValue());
                    int t0 = AccountID.intValue() % LogicEventData.getEvent(0).getGroupSize().intValue();
                    Object t1 = (List)var18.get(Integer.valueOf(t0));
                    if(t1 == null) {
                        t1 = new ArrayList();
                        var18.put(Integer.valueOf(t0), t1);
                    }

                    ((List)t1).add(AccountID);
                }
            }

            for(triple = 0; triple < accounts.size(); ++triple) {
                if(((Login)accounts.get(triple)).PassToken != null && ((Login)accounts.get(triple)).PassToken.length() > 0) {
                    AccountID = Long.valueOf(((Login)accounts.get(triple)).AccountID.longValue());
                    Integer var21 = Integer.valueOf(AccountID.intValue() % LogicEventData.getEvent(0).getGroupSize().intValue());
                    List var23 = triples.getIndexesByGroup(var21);
                    if(var23 != null) {
                        List t2 = (List)var18.get(var21);
                        if(t2 != null && this.isIdinGroup(t2, AccountID)) {
                            Iterator cfg = var23.iterator();

                            while(cfg.hasNext()) {
                                List cfgname = (List)cfg.next();
                                List e = (List)var18.get(cfgname.get(0));
                                List ids1 = (List)var18.get(cfgname.get(1));
                                if(e != null && e.size() > 0 && ids1 != null && ids1.size() > 0) {
                                    this.removeIdFromGroup(t2, AccountID);
                                    ArrayList gl = new ArrayList();
                                    gl.add(AccountID);
                                    gl.add((Long)e.remove(0));
                                    gl.add((Long)ids1.remove(0));
                                    accountGroups.add(gl);
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            Iterator var20 = accountGroups.iterator();

            while(var20.hasNext()) {
                List var19 = (List)var20.next();
                Login var22 = this.getAccountById((Long)var19.get(0));
                Login var24 = this.getAccountById((Long)var19.get(1));
                Login var25 = this.getAccountById((Long)var19.get(2));
                String var26 = var22.FarmName + "." + var24.FarmName + "." + var25.FarmName + ".cfg";
                cfgFile var27 = new cfgFile(var26);
                this.writeToConfig(0, var27, var22, var22.FarmName, true);
                this.writeToConfig(1, var27, var24, var24.FarmName, true);
                this.writeToConfig(2, var27, var25, var25.FarmName, true);
                var27.setProperty("profile.size", "3");

                try {
                    var27.cfgstore("Farm triple config, generated automaticaly.");
                } catch (FileNotFoundException var14) {
                    var14.printStackTrace();
                } catch (IOException var15) {
                    var15.printStackTrace();
                }
            }

            System.out.println(Logger.formatPT("Collector", "  Triples selection process... Done."));
        }

    }

    private boolean isIdinGroup(List<Long> grp, Long id) {
        Iterator var4 = grp.iterator();

        while(var4.hasNext()) {
            Long gid = (Long)var4.next();
            if(gid.longValue() == id.longValue()) {
                return true;
            }
        }

        return false;
    }

    private void removeIdFromGroup(List<Long> grp, Long id) {
        for(int i = 0; i < grp.size(); ++i) {
            Long gid = (Long)grp.get(i);
            if(gid.longValue() == id.longValue()) {
                grp.remove(i);
                break;
            }
        }

    }

    private Login getAccountById(Long id) {
        Iterator var3 = accounts.iterator();

        while(var3.hasNext()) {
            Login l = (Login)var3.next();
            if(l.AccountID.longValue() == id.longValue()) {
                return l;
            }
        }

        return null;
    }

    private Login createNewFarm(Integer version, String masterhash) {
        Login lmsg = new Login();
        lmsg.AccountID = new LogicLong(0L);
        lmsg.PassToken = null;
        if(masterhash != null) {
            lmsg.ResourceFingerPrint = masterhash;
        } else {
            lmsg.ResourceFingerPrint = "1ed0f3cb6bbc25c5c4855fc5dc5567979aa8a754";
        }

        if(version != null) {
            lmsg.VersionNumber = version.intValue();
        } else {
            lmsg.VersionNumber = 1078368;
        }

        lmsg.UDID = "";
        byte[] farmid = RandomIdentifiers.getNextAndroidID();
        lmsg.OpenUDID = StringUtils.hexToString(farmid);
        lmsg.MacAddress = StringUtils.macAddrStyle(RandomIdentifiers.getNextMACAddress());
        lmsg.Device = "Sony Xperia Z2";
        lmsg.ADID = RandomIdentifiers.getNextAdvertisingID(farmid);
        lmsg.add123present = 1;
        lmsg.FirmwareVersion = "4.4";
        lmsg.add1 = "";
        lmsg.add2 = lmsg.OpenUDID;
        lmsg.add3 = "EN";
        lmsg.add4 = "";
        lmsg.add5 = Byte.valueOf((byte)1);
        lmsg.add6 = "";
        lmsg.add7 = Integer.valueOf(2);
        return lmsg;
    }

    public void run() {
        System.out.println(Logger.formatPT("Collector", "  thread FarmsWorker started."));
        ArrayList cur = new ArrayList();
        List startLevelUpTime = accounts;
        synchronized(accounts) {
            Iterator idx = accounts.iterator();

            while(true) {
                if(!idx.hasNext()) {
                    break;
                }

                Login timedout = (Login)idx.next();
                cur.add(timedout);
            }
        }

        System.out.println(Logger.formatPT("Collector", "  create " + cur.size() + " workers."));

        Integer var9;
        for(int var7 = 0; var7 < cur.size(); ++var7) {
            var9 = null;
            String var10 = this.profiles.getProperty(String.format("profile.tutorial.data.%d", new Object[]{Integer.valueOf(var7)}));
            if(var10 != null && var10.length() > 0) {
                var9 = Integer.valueOf(var10);
            }

            SocksProxy w = null;
            if(Louncher.socksip != null && Louncher.socksport != null && Louncher.socksip.length() > 0 && Louncher.socksport.length() > 0) {
                w = new SocksProxy(Louncher.socksip, Louncher.socksport, "game.haydaygame.com", "9339", false);
            }

            workers.add(new Worker(Integer.valueOf(var7), getAccount(var7), w, var9, getAccount(var7).FarmName, Boolean.valueOf(false)));
        }

        Date var8 = new Date();
        System.out.println(Logger.formatPT("Collector", "  waiting to all workers done."));
        var9 = Integer.valueOf(0);

        label72:
        while(!this.dostop) {
            this.sleep(1000L);
            if(this.allDone()) {
                break;
            }

            if((new Date()).getTime() - var8.getTime() >= 780000L) {
                Iterator var13 = workers.iterator();

                while(true) {
                    if(!var13.hasNext()) {
                        break label72;
                    }

                    Worker var11 = (Worker)var13.next();
                    if(!var11.getDone()) {
                        var9 = Integer.valueOf(var9.intValue() + 1);
                        var11.stop();
                    }
                }
            }
        }

        System.out.println(Logger.formatPT("Collector", "  " + (cur.size() - var9.intValue()) + " workers complete."));
        this.sleep(1000L);
        this.sleep(1000L);
        this.sleep(1000L);
        this.sleep(1000L);
        this.sleep(1000L);
        this.sleep(1000L);
        this.sleep(1000L);
        this.sleep(1000L);
        this.sleep(1000L);
        this.doTriplesOnExit = true;
        System.out.println(Logger.formatPT("Collector", "  Accept all tutorials to enable pedestrains..."));
        if(workers.size() > 0) {
            for(int var12 = 0; var12 < workers.size(); ++var12) {
                Worker var14 = (Worker)workers.get(var12);
                if(var14.getDone()) {
                    var14.setCompleteTutorialsMode();
                    var14.startAccountWorker();
                }
            }
        }

    }

    private boolean allDone() {
        Iterator var2 = workers.iterator();

        while(var2.hasNext()) {
            Worker w = (Worker)var2.next();
            if(!w.getDone()) {
                return false;
            }
        }

        return true;
    }

    private long sleep(long mills) {
        long finalTime = (new Date()).getTime() + mills;
        if((new Date()).getTime() < finalTime) {
            try {
                long waiting = finalTime - (new Date()).getTime();
                if(waiting > 1000L) {
                    waiting = 1000L;
                }

                if(waiting > 0L) {
                    Thread.sleep(waiting);
                }
            } catch (InterruptedException var7) {
                ;
            }

            return finalTime - (new Date()).getTime();
        } else {
            return 0L;
        }
    }
}
